<template>
  <div class="mod-config">
    <el-form :inline="true" :model="params" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input
          v-model="params.key"
          placeholder="项目名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="pageResult.records"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="insId"
        header-align="center"
        align="center"
        label="序号"
      >
      </el-table-column>
      <el-table-column
        prop="inspectionName"
        header-align="center"
        align="center"
        label="项目名称"
      >
      </el-table-column>
      <el-table-column
        prop="className"
        header-align="center"
        align="center"
        label="项目类别"
      >
      </el-table-column>
      <el-table-column
        prop="unitPrice"
        header-align="center"
        align="center"
        label="项目单价"
      >
      </el-table-column>
      <el-table-column
        prop="cost"
        header-align="center"
        align="center"
        label="项目成本"
      >
      </el-table-column>
      <el-table-column
        prop="unit"
        header-align="center"
        align="center"
        label="单位"
      >
      </el-table-column>
      <el-table-column  fixed="right"  header-align="center"  align="center"  width="150" label="操作">
        <template slot-scope="scope">
          <el-button  type="primary"  size="small"  @click="addOrUpdateHandle(scope.row.insId)">修改</el-button>
          <el-button  type="primary" size="small" @click="deleteHandle(scope.row.insId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageResult.current"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageResult.pages"
      :total="pageResult.total"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./hisinspectionfee-add-or-update";

import { InspectService } from "../../model/zyj/InspectService.js";
const inspectService = InspectService.getInstance();

export default {
  data() {
    return {
      params: {
        key: "",
        size: 5,
        page: 1,
      },
      pageResult: {},
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  created() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      inspectService.pageQuery(this.params).then(({ data }) => {
        if (data && data.code === 0) {
          this.pageResult = data.data;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.params.size = val;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.params.page = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.insId;
          });
      this.$confirm(
        `是否确定删除`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        inspectService.removeByIds(ids).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.nessage);
          }
        });
      });
    },
  },
};
</script>
